<?php

class Users {

    private $_ID;
    private $_userName;
    private $_fullname;
    private $_email;
    private $_password;
    private $_status;

    public function getID() {
        return $this->_ID;
    }

    public function setID($ID) {
        $this->_ID = $ID;
    }

    public function getUserName() {
        return $this->_userName;
    }

    public function setUserName($UserName) {
        $this->_userName = $UserName;
    }

    public function getFullname() {
        return $this->_fullname;
    }

    public function setFullname($Fullname) {
        $this->_fullname = $Fullname;
    }

    public function getEmail() {
        return $this->_email;
    }

    public function setEmail($Email) {
        $this->_email = $Email;
    }

    public function getPassword() {
        return $this->_password;
    }

    public function setPassword($Password) {
        $this->_password = $Password;
    }

    public function getStatus() {
        return $this->_status;
    }

    public function setStatus($Status) {
        $this->_status = $Status;
    }

    // get user by userID
    public function getByID($db) {
        if ($db != null) {
            $sql = 'SELECT
                        `U_ID`,
                        `U_UserName`,
                        `U_FullName`,
                        `U_Email`,
                        `U_PassWord`,
                        `U_Status`
                      FROM `users`
                      WHERE U_ID = :U_ID';

            $parameters = array(':U_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);
            $result = $db->executeQuery($sql, $parameters, $values, $types);

            // nếu có kết quả
            if ($result != null) {

                $row = $result->fetch();

                $this->setID($row->U_ID);
                $this->setUserName($row->U_UserName);
                $this->setFullname($row->U_FullName);
                $this->setEmail($row->U_Email);
                $this->setPassword($row->U_PassWord);
                $this->setStatus($row->U_Status);



                return $this;
            } else {
                return null;
            }
        }
    }

    public function getByUserName($db) {
        if ($db != null) {
            $sql = 'SELECT
                        `U_ID`,
                        `U_UserName`,
                        `U_FullName`,
                        `U_Email`,
                        `U_PassWord`,
                        `U_Status`
                      FROM `users`
                      WHERE U_UserName = :U_UserName';

            $parameters = array(':U_UserName');
            $values = array($this->getUserName());
            $types = array(PDO::PARAM_STR);
            $result = $db->executeQuery($sql, $parameters, $values, $types);

            // nếu có kết quả
            if ($result != null) {
                $row = $result->fetch();
                $this->setID($row->U_ID);
                $this->setUserName($row->U_UserName);
                $this->setFullname($row->U_FullName);
                $this->setEmail($row->U_Email);
                $this->setPassword($row->U_PassWord);
                $this->setStatus($row->U_Status);
                return $this;
            } else {
                return null;
            }
        }
    }

    public function getList($db = null) {
        if ($db != null) {
            $sql = 'SELECT
                        `U_ID`,
                        `U_UserName`,
                        `U_FullName`,
                        `U_Email`,
                        `U_PassWord`,
                        `U_Status`
                      FROM `users`
                      ORDER BY U_ID DESC';

            $parameters = null;
            $values = null;
            $types = null;
            $result = $db->executeQuery($sql, $parameters, $values, $types);

            // nếu có kết quả
            if ($result != null) {
                
                // mảng danh sách trả về
                $rows = array();

                // lấy mỗi dòng dữ liệu đưa vào mảng danh sách trả về
                while ($row = $result->fetch()) {

                    $user = new Users();
                    $user->setID($row->U_ID);
                    $user->setUserName($row->U_UserName);
                    $user->setFullname($row->U_FullName);
                    $user->setEmail($row->U_Email);
                    $user->setPassword($row->U_PassWord);
                    $user->setStatus($row->U_Status);

                    $rows[] = $user;
                }

                return $rows;
            } else {
                return null;
            }
        }
    }

    public function add($db = null) {
        if ($db != null) {
            $sql = "INSERT INTO `users`
                                (`U_UserName`,
                                 `U_FullName`,
                                 `U_Email`,
                                 `U_PassWord`,
                                 `U_Status`)
                    VALUES (:U_UserName,
                            :U_FullName,
                            :U_Email,
                            :U_PassWord,
                            :U_Status);";

            $parameters = array(
                ':U_UserName', ':U_FullName',
                ':U_Email', ':U_PassWord',
                ':U_Status'
            );

            $values = array(
                $this->getUserName(), $this->getFullname(),
                $this->getEmail(), $this->getPassword(),
                $this->getStatus()
            );

            $types = array(
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_INT
            );
            return $db->executeNonQuery($sql, $parameters, $values, $types, true);
        }
    }

    public function update($db = null) {

        if ($db != null) {
            $sql = "UPDATE `users`
                    SET `U_FullName` = :U_FullName,
                        `U_Email` = :U_Email,
                        `U_PassWord` = :U_PassWord,
                        `U_Status` = :U_Status
                    WHERE `U_ID` = :U_ID;";

            $parameters = array(
                ':U_FullName',
                ':U_Email',
                ':U_PassWord',
                ':U_Status',
                ':U_ID',
            );

            $values = array(
                $this->getFullname(),
                $this->getEmail(),
                $this->getPassword(),
                $this->getStatus(),
                $this->getID()
            );

            $types = array(
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_INT,
                PDO::PARAM_INT,
            );

            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }

    public function delete($db = null) {

        if ($db != null) {
            $sql = "DELETE
                    FROM `users`
                    WHERE `U_ID` = :U_ID;";

            $parameters = array(':U_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);
            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }

    public function updateStatus($db = null) {
        if ($db != null) {
            $sql = "UPDATE `users`
                    SET 
                      `U_Status` = :U_Status
                    WHERE `U_ID` = :U_ID;";

            $parameters = array(':U_Status', ':U_ID');
            $values = array($this->getStatus(), $this->getID());
            $types = array(PDO::PARAM_INT, PDO::PARAM_INT);
            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }

}

?>
